package impl.repository;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JOptionPane;

import dao.AccessDatabase;
import domain.Address;
import domain.Customer;
import exception.InvalidCepException;
import interfaces.repository.AddressRepository;

public class StubAddresRepositoryImpl implements AddressRepository {

	@Override
	public Address findByCustomer(Customer customer) {
		Address endereco = null;
		try {
			AccessDatabase a = new AccessDatabase();
			Connection con = a.conectar();
			Statement st = con.createStatement();
			ResultSet rs = st.executeQuery("SELECT * FROM address WHERE idCustomer='"+customer.getId()+"'");
			
				endereco = new Address(
						
						rs.getInt("idCustomer"),
						rs.getString("street"),
						rs.getString("city"),
						rs.getString("state"),
						rs.getString("cep"), 
						rs.getString("comp")) 
				{};

				
		
			con.close();

			return endereco;
		} catch (SQLException e) {
			return null;
		}
		
	}

	@Override
	public boolean add(Address address) {
		try{
			AccessDatabase a = new AccessDatabase();
         	Connection con = a.conectar();
            Statement st = con.createStatement();
            st.execute("INSERT INTO `address` (`idCustomer`,`street`,`city`,`state`,`cep`,`comp`) VALUES ('"+address.getIdCustomer()+"','"+address.getStreet()+"','"+address.getCity()+"','"+address.getState()+"','"+address.getCep()+"','"+address.getComp()+"');");
            con.close();
                    return true;
		}catch(SQLException e){
			System.out.println("nao foi possivel prosseguir com o registro" + e);
		}
		return false;
	}

	@Override
	public boolean edit(Address before, Address after) {
		try{
			 
         	 AccessDatabase a = new AccessDatabase();
         	 Connection con = a.conectar();
            Statement st = con.createStatement();
            st.execute("UPDATE `address` SET `street`='"+after.getStreet()+"', `city`='"+after.getCity()+"', `state`='"+after.getState()+"', `cep`='"+after.getCep()+"', `comp`='"+after.getComp()+"' WHERE `idAddress`='"+before.getId()+"'");
            con.close();
              return true;
            }catch(Exception t){
            	JOptionPane.showMessageDialog(null, t.getMessage());
            	return false;
              }
		
	}

	@Override
	public boolean delete(Address address) {
		// TODO Auto-generated method stub
		return false;
	}

}
